home *** CD-ROM | disk | FTP | other *** search
/ The Best of MacTutor - S…e Code for Volumes 1 to 5 / The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin / Source Code / #26 (Nov 87) / asm lab Formatter / NumToString.ROM.asm < prev   
Assembly Source File  |  1987-10-21  |  2KB  |  68 lines

  1. ; NOTE: This code is Apple Computer's. Only the comments are mine.
  2.  
  3. ;==========
  4. NumToString
  5. ;==========
  6. ; convert a 32 bit longint into a pascal string
  7. ;  input: D0 longint
  8. ;      A0 points to a space of at least 12 bytes
  9. ; output: A0 points to pascal string
  10.  
  11.     MOVEM.L        D0-D6/A1,-(SP)
  12.     MOVEQ        #0,D1        ;init digits
  13.     MOVEQ        #0,D2
  14.     MOVEQ        #0,D3
  15.     MOVEQ        #0,D4
  16.     MOVEQ        #0,D5
  17.     MOVEQ        #31,D6        ;loop counter
  18.     LEA        1(A0),A1    ;skip length byte
  19.     TST.L        D0        ;is num zero?
  20.     BGT.S        @2
  21.     BMI.S        @1
  22.     MOVE.B        #'0',(A1)+    ;special case for zero
  23.     BRA.S        @3
  24. @1    MOVE.B        #'-',(A1)+    ;give string a minus sign
  25.     NEG.L        D0        ;make number positive
  26. @2    ADD.L        D0,D0        ;shift a bit into extend flag
  27.     ABCD        D5,D5        ;tens' & ones' digits
  28.     ABCD        D4,D4        ;thous' & hunds' digits
  29.     ABCD        D3,D3        ;hund thous' & ten thous' digits
  30.     ABCD        D2,D2        ;ten mils' & mils' digits
  31.     ABCD        D1,D1        ;bils' & hund mils' digits
  32.     DBRA        D6,@2        ;do next bit
  33. ;NOTE: at this point D6 = -1. It is used as a flag to kill leading zeros.
  34.     BSR.S        Do2Digits
  35.     MOVE.B        D2,D1
  36.     BSR.S        Do2Digits
  37.     MOVE.B        D3,D1
  38.     BSR.S        Do2Digits
  39.     MOVE.B        D4,D1
  40.     BSR.S        Do2Digits
  41.     MOVE.B        D5,D1
  42.     BSR.S        Do2Digits
  43. ;calculate length of string that was created
  44. @3    MOVE        A1,D0        ;end of string + 1
  45.     SUB        A0,D0        ;beginning of string
  46.     SUBQ.B        #1,D0        ;minus 1 for length byte
  47.     MOVE.B        D0,(A0)
  48.     MOVEM.L        (SP)+,A1/D0-D6
  49.     RTS
  50.  
  51. Do2Digits
  52. ;convert BCD byte in D1 into 2 ASCII digits.
  53. ;do most significant digit
  54.     ROR        #4,D1
  55.     BSR.S        DoADigit
  56. ;do least significant digit
  57.     ROL        #4,D1
  58. DoADigit
  59.     TST        D6        ;have we had a non-zero digit yet?
  60.     BPL.S        @6
  61.     TST.B        D1        ;is this a leading zero?
  62.     BEQ.S        @7
  63.     MOVEQ        #0,D6        ;print all zeros from now on
  64. @6    ORI.B        #$30,D1        ;covert BCD digit to ASCII
  65.     MOVE.B        D1,(A1)+    ;add it to the string
  66.     SUB.B        D1,D1
  67. @7    RTS
  68.